
export function base64PlayUrl (base64String) {
		
    // console.log(base64String);
    const binaryString = atob(base64String);
    
    const binaryLength = binaryString.length;
    const bytes = new Uint8Array(binaryLength);
    for (let i = 0; i < binaryLength; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }

    var arr = bytes;

    var getVal = function (cls, def, name, min, max, range) {
        var val = + cls || def;
        if (range && range.indexOf(+val) == -1 || !range && (val < min || val > max)) {
            throw new Error("乱填" + name + "：" + val + (range ? "，" + name + "取值范围：" + JSON.stringify(range) : ""));
        }
        return val;
    };
    try {
        var sampleRate = 12000;
        var bitRate = 16;
        var numChannels = 1;

        var sampleRateNew = getVal(0, sampleRate, "新采样率", 6000, 9999999999);
        var bitRateNew = getVal(0, Math.min(bitRate, 16), "新位数", 0, 0, [8, 16]);
        var numChannelsNew = getVal(0, numChannels, "新声道数", 0, 0, [1, 2]);

        var subA = getVal(0, 0, "截取开始时间", 0, 9999999999);
        var subB = getVal(0, 0, "截取结束时间", 0, 9999999999);
    } catch (e) {
        return;
    };


    if (bitRate == 16) {
        var res = new Int16Array(arr.buffer);
    } else if (bitRate == 8) {
        var res = new Int16Array(arr.length);
        for (var j = 0; j < arr.length; j++) {
            var b = arr[j];
            res[j] = (b - 128) << 8;
        };
    } else if (bitRate == 24) {
        var res = new Int16Array(arr.length / 3);
        for (var i = 0, j = 0; j < arr.length;) {
            var n = ((arr[j++] | (arr[j++] << 8) | (arr[j++] << 16)) << 8) >> 8;
            n = n / 16777216;
            res[i++] = n * 0x7FFF;
        };
    }


    var resA = res;
    var resB = res;
    if (numChannels == 2) {
        //分离声道
        resA = new Int16Array(res.length / 2);
        resB = new Int16Array(res.length / 2);
        for (var i = 0; i < resA.length; i++) {
            resA[i] = res[i * 2];
            resB[i] = res[i * 2 + 1];
        };
    };


    //降低采样率
    if (sampleRate > sampleRateNew) {
        resA = Recorder.SampleData([resA], sampleRate, sampleRateNew).data;
        resB = Recorder.SampleData([resB], sampleRate, sampleRateNew).data;
        sampleRate = sampleRateNew;
    };

    //截取
    if (subA || subB) {
        resA = resA.subarray(Math.round(subA * sampleRate / 1000), subB ? Math.round(subB * sampleRate / 1000) : resA.length);
        resB = resB.subarray(Math.round(subA * sampleRate / 1000), subB ? Math.round(subB * sampleRate / 1000) : resB.length);
    };
    var size = resA.length;
    var duration = Math.floor(size / sampleRate * 1000);

    //编码数据 
    var dataLength = size * (bitRateNew / 8) * numChannelsNew;
    var buffer = new ArrayBuffer(44 + dataLength);
    var data = new DataView(buffer);

    var offset = 0;
    var writeString = function (str) {
        for (var i = 0; i < str.length; i++, offset++) {
            data.setUint8(offset, str.charCodeAt(i));
        };
    };
    var write16 = function (v) {
        data.setUint16(offset, v, true);
        offset += 2;
    };
    var write32 = function (v) {
        data.setUint32(offset, v, true);
        offset += 4;
    };

    /* RIFF identifier */
    writeString('RIFF');
    /* RIFF chunk length */
    write32(36 + dataLength);
    /* RIFF type */
    writeString('WAVE');
    /* format chunk identifier */
    writeString('fmt ');
    /* format chunk length */
    write32(16);
    /* sample format (raw) */
    write16(1);
    /* channel count */
    write16(numChannelsNew);
    /* sample rate */
    write32(sampleRate);
    /* byte rate (sample rate * block align) */
    write32(sampleRate * (numChannelsNew * bitRateNew / 8));
    /* block align (channel count * bytes per sample) */    
    write16(numChannelsNew * bitRateNew / 8);
    /* bits per sample */
    write16(bitRateNew);
    /* data chunk identifier */
    writeString('data');
    /* data chunk length */
    write32(dataLength);
    // 写入采样数据
    if (bitRateNew == 8) {
        for (var i = 0; i < size; i++, offset++) {
            //16转8
            var val = (resA[i] >> 8) + 128;
            data.setInt8(offset, val, true);
            if (numChannelsNew == 2) {
                offset++;
                val = (resB[i] >> 8) + 128;
                data.setInt8(offset, val, true);
            };
        };
    } else {
        for (var i = 0; i < size; i++, offset += 2) {
            data.setInt16(offset, resA[i], true);
            if (numChannelsNew == 2) {
                offset += 2;
                data.setInt16(offset, resB[i], true);
            };
        };
    };

    var blob = new Blob([data.buffer], { type: "audio/wav" });
    var url = (window.URL || webkitURL).createObjectURL(blob);
    
    return url
};

